// 数组的reduce方法，会循环当前的数组，侧重于进行滚雪球的操作
// 数组.reduce（函数，初始值）
// 数组.reduce（（上次计算的结果，当前循环的Item项）=> {return 具体操作},0）

// 下次操作的初始值，依赖与上次操作的返回值，用reduce
let arr = [1,2,3,4,5,6,7,8,9,10]
let sum = arr.reduce((val,item) => {return item + val},0) //0表示初始值
console.log(sum);

// 链式获取对象属性
const obj = {
    name: 'bruce',
    info: {
        address:{
            location:'北京顺义'
        }
    }
}

const attrs = ["info","address","location"];

const location = attrs.reduce((newObj,key) =>newObj[key] ,obj)
console.log(location);

const attrs1 = 'info.address.location';
const location1 = attrs1.split(".").reduce((newObj,key) => newObj[key],obj)
console.log(location1);
